package vfc.mapping
{
    import com.yahoo.maps.api.YahooMap;
    import com.yahoo.maps.api.YahooMapEvent;
    import com.yahoo.maps.api.core.location.Address;
    import com.yahoo.maps.api.core.location.LatLon;

    import flash.events.Event;
    import flash.events.MouseEvent;

    import mate.constants.YahooMapAPIKey;
    import mate.events.MapEvent;
    import mate.events.SiteEvent;

    import mx.core.UIComponent;

    import vfc.model.valueObjects.Site;

    [Event(name="mapInitialize", type="com.yahoo.maps.api.YahooMapEvent")]
    public class UIYahooMap extends UIComponent
    {

        public var yahooMap:YahooMap;

        public function UIYahooMap():void
        {
            super();

        }

        protected override function commitProperties():void{
        	super.commitProperties();
        	if(_sitesChanged){
        		yahooMap.markerManager.removeAllMarkers();
  				for each (var site:Site in _sites){

  					this.addCustomMarkerByAddress(new HomeMarker(site, 0x1c436a,0x1c436a,0x000000, site.name + "\n" + site.address.split(",")[0],"assets/images/soup_bowl_map.png"),
  													"test",
  													site.address);

  					//trace(address);
  					this.setCenterByAddress(site.address);
      			}
        	}
       }

        protected var _sitesChanged:Boolean = false;
        protected var _sites:Array = new Array();
        public function set sites(value:Array):void{
        	_sites = value;
        	_sitesChanged = true;
        	invalidateProperties();
        }
        protected override function createChildren():void
        {
            super.createChildren();

            if(yahooMap == null)
            {
                yahooMap = new YahooMap();
                yahooMap.addEventListener(YahooMapEvent.MAP_INITIALIZE, onMapInit);
                //yahooMap.addEventListener(YahooMapEvent.MAP_DRAG_START, handleMapMove);
                yahooMap.addEventListener(YahooMapEvent.MAP_DRAG, handleMapMove);
                //yahooMap.addEventListener(YahooMapEvent.MAP_MOVE, handleMapMove);
                //yahooMap.addEventListener(YahooMapEvent.MAP_ZOOM, handleMapMove);
                //this.addEventListener(MouseEvent.CLICK, handleMapMove);
                yahooMap.init(YahooMapAPIKey.yahooMapApiKey, 100, 100);
                addChild(yahooMap);
                yahooMap.addPanControl();
                yahooMap.addTypeWidget();
                yahooMap.addScaleBar();
                yahooMap.addZoomWidget();

            }
        }
		protected function handleMapMove(e:Event):void{
			trace("[UIYahooMap] - Panned");
			dispatchEvent( new MapEvent(MapEvent.PANNED));
		}
        protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);

            yahooMap.setSize(unscaledWidth, unscaledHeight);
        }

        protected function onMapInit(event:YahooMapEvent):void
        {
        	//yahooMap.zoomLevel = 7;
            // bubble it up
            dispatchEvent(new YahooMapEvent(YahooMapEvent.MAP_INITIALIZE));

            //yahooMap.centerLatLon = new LatLon(37.779160,-122.420049);
            yahooMap.centerLatLon = new LatLon(37.295445, -121.927691);
            yahooMap.zoomLevel = 5;
        }

        public function setCenterByAddress(add:String):void
        {
        	yahooMap.setCenterAddress(new Address(add));
        }

        // helper function
        public function addMarkerByLatLon(lat:Number, lon:Number, name:String):void
        {
            var marker:HomeMarker         = new HomeMarker(null);
            marker.latlon                  = new LatLon(lat, lon);
            marker.map                     = yahooMap;
            yahooMap.markerManager.addMarker(marker);
        }

        public function addCustomMarkerByAddress(marker:HomeMarker, name:String, add:String):void
        {
        	//marker = new Marker();
            marker.groupId            = name;
            marker.address			  = new Address(add);
            marker.map                = yahooMap;
            marker.addEventListener(MouseEvent.CLICK, handleMarkerClicked, false, 0, true);
            yahooMap.markerManager.addMarker(marker);
        }

        public function handleMarkerClicked(event:MouseEvent):void{
        	//dispatch Event
        	var site:Site = HomeMarker(event.currentTarget).site;
        	this.setCenterByAddress(site.address);
			dispatchEvent(new SiteEvent(SiteEvent.SITE_SELECTED, site));
        }

    }
}

